package com.oocl.aeat.util;
import java.sql.Connection;

import org.apache.commons.dbcp.BasicDataSource;
public class DataSourceUtils {
	
	private static BasicDataSource ds;
	//声明ThreadLocal容器对象
	private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
	static {
		ds = new BasicDataSource();
		ds.setUrl( ServerConfig.JDBC_DRIVERURL );
		ds.setDriverClassName( ServerConfig.JDBC_DRIVERCLASS );
		ds.setUsername( ServerConfig.JDBC_USERNAME );
		ds.setPassword( ServerConfig.JDBC_PASSWORD );
		ds.setMaxActive(50);
		ds.setMinIdle(8);
		ds.setMaxIdle(20);
		ds.setRemoveAbandoned(true);
		ds.setRemoveAbandonedTimeout(10);
		
	}
	
	public static BasicDataSource getDataSource() {
		return ds;
	}
	
	//提供一个返回Connection的方法
	public static Connection getConn(){
		Connection con = null;
		//先从tl中获取数据
		con = tl.get();
		if(con==null){
			try{
				con = getDataSource().getConnection();
				//放到tl
				tl.set(con);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return con;
	}
	public static void remove() {
		tl.remove();
	}
}
